import CryptoJS from 'crypto-js'
// const CryptoJS = require('crypto-js'); //引用AES源码js
const key = CryptoJS.enc.Utf8.parse(uni.getStorageSync('user_info') ? 'Aa123456' : "9ouxQY9ALwFhQaFj"); //十六位十六进制数作为
const iv = CryptoJS.enc.Utf8.parse('FQFBCcQh59HNFr2M'); //十六位十六进制数作为密钥偏移量
//加密方法
export function Encrypt(word) {
  // const key = CryptoJS.enc.Utf8.parse(uni.getStorageSync('user_info') ? 'Aa123456' : "9ouxQY9ALwFhQaFj");
  // console.log('word     is:'+JSON.stringify(word)) ;
  word = JSON.stringify(word);
  let srcs = CryptoJS.enc.Utf8.parse(word);
  let encrypted = CryptoJS.AES.encrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
  return encrypted.toString();
}


//解密方法
export function Decrypt(word) {
  //CryptoJS.enc.Utf8.parse(uni.getStorageSync('user_info') ? 'Aa123456' : "9ouxQY9ALwFhQaFj"); //十六位十六进制数作为
  const key = CryptoJS.enc.Utf8.parse(key);
  let decrypted = {};
  decrypted = CryptoJS.AES.decrypt(word, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return decrypted.toString();
}

export default {
  Decrypt,
  Encrypt
}
